<template>
  <div style="display: flex;  flex-direction: column;">
    <div style="display: flex; max-height: 20px">
      <h5>采购方式</h5>
      <el-select v-model="procurementMethod" placeholder="请选择" style="margin-top: -5px;margin-left: 10px; width: 120px">
        <el-option :key="''" :label="'全部'" :value="''"></el-option>
        <el-option :key="'磋商'" :label="'磋商'" :value="'磋商'"></el-option>
        <el-option :key="'委托'" :label="'委托'" :value="'委托'"></el-option>
        <el-option :key="'竞价'" :label="'竞价'" :value="'竞价'"></el-option>
      </el-select>
      <h5 style="margin-left: 30px">项目编号</h5>
      <el-input style="margin-top: -5px; width:120px; margin-left: 10px" placeholder="请输入项目编号" v-model="projectNumber"></el-input>
      <h5 style="margin-left: 30px">项目名称</h5>
      <el-input style="margin-top: -5px; width:120px; margin-left: 10px" placeholder="请输入项目名称" v-model="projectName" ></el-input>

      <el-button type="primary" style="margin-top: -5px; margin-left: 30px" icon="el-icon-search" @click="init">查询</el-button>
      <el-button type="warning" style="margin-top: -5px; margin-left: 10px" icon="el-icon-refresh" @click="reset">重置</el-button>
    </div>
    <div style="display: flex;">
      <el-table :data="tableData" style="width: 100%; margin-top: 30px" height="500">
        <el-table-column fixed label="序号" width="50">
          <template slot-scope="scope">
            {{ scope.$index + 1 }}
          </template>
        </el-table-column>
        <el-table-column prop="projectNumber" label="项目编号" width="120"></el-table-column>
        <el-table-column prop="projectName" label="项目名称" width="150"></el-table-column>
        <el-table-column prop="quantity" label="数量" width="50"></el-table-column>
        <el-table-column prop="budget" label="预算（万元）" width="100"></el-table-column>
        <el-table-column prop="state" label="状态" width="70">
          <template slot-scope="scope">
            <el-tag v-if="scope.row.status < 2" type="warning" effect="circle">待公告</el-tag>
            <el-tag v-else-if="scope.row.status >= 2" type="success" effect="circle">已完成</el-tag>
            <span v-else>未知</span>
          </template>
        </el-table-column>
        <el-table-column prop="procurementType" label="采购类型" width="80">
          <template slot-scope="scope">
            <span v-if="scope.row.procurementType === '公开采购'">公开采购</span>
            <span v-else-if="scope.row.procurementType === '单一来源'">单一来源</span>
            <span v-else>未知</span>
          </template>
        </el-table-column>
        <el-table-column prop="fundingSource" label="资金来源" width="80">
          <template slot-scope="scope">
            <span v-if="scope.row.fundingSource === '预算'">预算</span>
            <span v-else-if="scope.row.fundingSource === '拨款'">拨款</span>
            <span v-else-if="scope.row.fundingSource === '其他'">其他</span>
            <span v-else>未知</span>
          </template>
        </el-table-column>
        <el-table-column prop="applyingDepartment" label="申请科室" width="100"></el-table-column>
        <el-table-column prop="applicant" label="申请人" width="100"></el-table-column>
        <el-table-column prop="procurementMethod" label="采购方式" width="100">
          <template slot-scope="scope">
            <span v-if="scope.row.procurementMethod === '磋商'">磋商</span>
            <span v-else-if="scope.row.procurementMethod === '委托'">委托</span>
            <span v-else-if="scope.row.procurementMethod === '竞价'">竞价</span>
            <span v-else>未知</span>
          </template>
        </el-table-column>
        <el-table-column prop="projectType" label="项目类型" width="100">
          <template slot-scope="scope">
            <span v-if="scope.row.projectType === '工程'">工程</span>
            <span v-else-if="scope.row.projectType === '耗材'">耗材</span>
            <span v-else-if="scope.row.projectType === '设备'">设备</span>
            <span v-else-if="scope.row.projectType === '维护'">维护</span>
            <span v-else>未知</span>
          </template>
        </el-table-column>
        <el-table-column label="操作" width="150">
          <template slot-scope="scope">
            <el-button v-if="scope.row.status >= 2" type="success" @click="handleLook(scope.row)">查看</el-button>
            <el-button v-if="scope.row.status < 2" type="primary" @click="handleFilePublic(scope.row)">生成文件发布</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <div style="padding: 10px 0;margin-left: 800px">
      <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="pageNum"
          :page-size="pageSize"
          :page-sizes="[10, 15, 20, 30]"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total">
      </el-pagination>
    </div>
<!--    生成公告-->
    <el-dialog :visible.sync="dialogFilePublicVisible"
               :custom-class="'slide-right-dialog'"
    >
      <div style="border: 2px solid #409EFF; border-radius: 5px; padding: 20px; background-color: #f2f7fc; margin-bottom: 20px;">
        <el-descriptions :title="announForm.title" style="display: inline-block;margin-top: -100px" column="4" size="mini">
          <el-descriptions-item label="采购方式">{{announForm.procurementMethod}}</el-descriptions-item>
          <el-descriptions-item label="项目预算（万元）">{{ announForm.budget }}</el-descriptions-item>
        </el-descriptions>
      </div>
      <div class="dialog-title"><i class="el-icon-d-arrow-right"></i>生成编制文件（公告）</div>
      <el-form :model="announForm" :rules="rules" :ref="announForm" label-width="100px" class="demo-ruleForm custom-form" style="margin-top: 30px">
        <el-form-item label="公告名称" prop="announcementName">
          <el-input v-model="announForm.announcementName"></el-input>
        </el-form-item>
        <el-form-item label="响应文件递交截止时间" prop="deadline">
          <el-date-picker
              v-model="announForm.deadline"
              type="datetime"
              placeholder="选择日期时间"
              :picker-options="pickerOptionsDead">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="磋商地点" prop="pofc">
          <el-input v-model="announForm.pofc"></el-input>
        </el-form-item>
        <el-form-item label="公告时间" prop="dateRange">
          <div class="block">
            <el-date-picker
                v-model="announForm.dateRange"
                type="daterange"
                align="right"
                unlink-panels
                range-separator="至"
                start-placeholder="开始日期"
                end-placeholder="结束日期"
                :picker-options="pickerOptions">
            </el-date-picker>
          </div>
        </el-form-item>
        <el-form-item label="公告附件" prop="announcementFileUrl">
          <el-upload
              :action="'http://'+serverIp() + ':8081/jdyfy/file/upload'"
              :show-file-list="true"
              :on-success="handleAnnounFile"
              :on-error="handleAnnounFileError">
            <el-button size="small" type="primary">点击上传</el-button>
            <div slot="tip" class="el-upload__tip">只能上传不超过50mb的文件</div>
          </el-upload>
        </el-form-item>
        <el-form-item label="公告内容" prop="announcementText">
          <el-input type="textarea" v-model="announForm.announcementText"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="save">确定</el-button>
          <el-input type="reset" style="width: 60px; margin-left: 10px;margin-right: 10px">重置</el-input>
          <el-button @click="dialogFilePublicVisible = false">取 消</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
<!--    查看-->
    <el-dialog :visible.sync="dialogViewVisible"
               :custom-class="'slide-right-dialog'"
    >
      <div class="dialog-title"><i class="el-icon-d-arrow-right"></i>查看采购项目</div>
      <el-form :model="form" label-width="100px" class="demo-ruleForm">
        <el-form-item label="项目编号">
          <el-input v-model="form.projectNumber" readonly></el-input>
        </el-form-item>
        <el-form-item label="项目名称">
          <el-input v-model="form.projectName" readonly></el-input>
        </el-form-item>
        <el-form-item label="数量">
          <el-select v-model="form.quantity" disabled>
            <el-option label="1" value="1"></el-option>
            <el-option label="2" value="2"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="项目预算(万元)">
          <el-input v-model="form.budget" readonly></el-input>
        </el-form-item>
        <el-form-item label="项目类型">
          <el-radio-group v-model="form.projectType" disabled>
            <el-radio-button :label="'工程'" :value="'工程'">工程</el-radio-button>
            <el-radio-button :label="'耗材'" :value="'耗材'">耗材</el-radio-button>
            <el-radio-button :label="'设备'" :value="'设备'">设备</el-radio-button>
            <el-radio-button :label="'维护'" :value="'维护'">维护</el-radio-button>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="采购方式">
          <el-radio-group v-model="form.procurementMethod" disabled>
            <el-radio-button :label="'磋商'" :value="'磋商'">磋商</el-radio-button>
            <el-radio-button :label="'委托'" :value="'委托'">委托</el-radio-button>
            <el-radio-button :label="'竞价'" :value="'竞价'">竞价</el-radio-button>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="采购类型">
          <el-radio-group v-model="form.procurementType" disabled>
            <el-radio-button :label="'公开采购'" :value="'公开采购'">公开采购</el-radio-button>
            <el-radio-button :label="'单一来源'" :value="'单一来源'">单一来源</el-radio-button>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="资金来源">
          <el-select v-model="form.fundingSource" disabled>
            <el-input label="预算" value="预算"></el-input>
            <el-input label="拨款" value="拨款"></el-input>
            <el-input label="其他" value="其他"></el-input>
          </el-select>
        </el-form-item>
        <el-form-item label="招标负责人" prop="tenderOfficer">
          <el-select v-model="form.tenderOfficer" placeholder="请选择" @change="onTenderOfficerChange" readonly>
            <el-option
                v-for="item in tenderOfficerOptions"
                :key="item.value"
                :label="item.label"
                :value="item.value">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="联系电话" prop="phoneNumber">
          <el-input v-model="form.phoneNumber" readonly></el-input>
        </el-form-item>
        <el-form-item label="联系人" prop="name">
          <el-select v-model="form.tenderOfficer" placeholder="请选择" @change="onTenderOfficerChange" readonly>
            <el-option
                v-for="item in tenderOfficerOptions"
                :key="item.value"
                :label="item.label"
                :value="item.value">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="联系科室" prop="department">
          <el-input v-model="form.department" readonly></el-input>
        </el-form-item>
        <el-form-item label="申请人">
          <el-select v-model="form.applicant" placeholder="请选择" @change="onApplicantChange">
            <el-option
                v-for="item in applicantOptions"
                :key="item.value"
                :label="item.label"
                :value="item.value">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="申请科室">
          <el-input v-model="form.applyingDepartment" readonly></el-input>
        </el-form-item>
        <el-form-item label="采购内容">
          <el-input type="textarea" v-model="form.procurementText" readonly></el-input>
        </el-form-item>
        <el-form-item>
          <el-button @click="dialogViewVisible = false">关闭</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
import {addProject, getProjectById, getProjectPageSort} from "@/api/project";
import {getUserByName} from "@/api/user";
import {addAnnouncement} from "@/api/announcement";
import {serverIp} from "../../../../public/config.js";

export default {
  name: "WeaveProj",
  data() {
    return {
      procurementMethod:'',
      projectNumber: "",
      projectName: "",
      tableData: [],
      pageNum: 1,
      total: 0,
      pageSize: 10,
      dialogFilePublicVisible: false,
      dialogViewVisible: false,
      pickerOptions: {
        shortcuts: [{
          text: '最近三天',
          onClick(picker) {
            const end = new Date();
            const start = new Date();
            end.setTime(end.getTime() + 3600 * 1000 * 24 * 3);
            picker.$emit('pick', [start, end]);
          }
        }]
      },
      pickerOptionsDead:{
        shortcuts: [{
          text: '三天后',
          onClick(picker) {
            const date = new Date();
            date.setTime(date.getTime() + 3600 * 1000 * 24 * 3);
            picker.$emit('pick', date);
          }
        }, {
          text: '一周后',
          onClick(picker) {
            const date = new Date();
            date.setTime(date.getTime() + 3600 * 1000 * 24 * 7);
            picker.$emit('pick', date);
          }
        }]
      },
      announForm: {
        dateRange: [],
        pofc:'西安市(某某医院行政院区招采办会议室）',
      },
      form_status: {},
      form: {
        fileUrl: '',
        projectNumber: '',
        projectName: '',
        quantity: 1,//数量
        budget: '',//预算
        projectType: '',
        procurementMethod: '',//采购方式
        procurementType: '',//采购类型
        fundingSource: '',
        tenderOfficer: '',//招标负责人
        phoneNumber: '',//联系电话
        name: '',//联系人
        department: '',//联系科室
        applyingDepartment: '',//申请科室
        applicant: '',//申请人
      },
    }
  },
  created() {
    this.init();
  },
  methods: {
    serverIp() {
      return serverIp
    },
    init() { // 初始化页面
      getProjectPageSort(this.pageNum, this.pageSize,this.projectNumber,this.projectName,this.procurementMethod)
          .then(res => {
            this.tableData = res.records;
            this.total = res.total;
          })
    },
    handleSizeChange(val) {//改变页码
      this.pageSize = val;
      this.init();
    },
    handleCurrentChange(val) {//改变每页显示条数
      this.pageNum = val;
      this.init();
    },
    handleFilePublic(row){ // 生成文件发布
      this.announForm = {
        pofc:'西安市(某某医院行政院区招采办会议室）'
        ,}
      this.announForm.announcementName = `关于{${row.projectNumber}+${row.projectName}}的{${row.procurementMethod}}公告`
      this.form_status.projectNumber = row.projectNumber
      this.announForm.projectNumber = row.projectNumber
      this.announForm.title = `${row.projectName}（招标项目编号${row.projectNumber}）`
      this.announForm.budget = row.budget
      this.announForm.procurementMethod = row.procurementMethod
      this.dialogFilePublicVisible=true;
    },
    save(){
      //格式化时间
      const dateStr = this.announForm.dateRange[0];
      const date = new Date(dateStr);
      const formattedDate = date.toISOString().slice(0, 10);
      const dateStr1 = this.announForm.dateRange[1];
      const date1 = new Date(dateStr1);
      const formattedDate1 = date1.toISOString().slice(0, 10);
      const dateStr2 = this.announForm.deadline;
      const date2 = new Date(dateStr2);
      const formattedDate2 = date2.toISOString().slice(0, 19).replace('T', ' ');
      this.announForm.announcementStartTime=formattedDate
      this.announForm.announcementEndTime=formattedDate1
      this.announForm.deadline=formattedDate2
      this.announForm.announcementFileUrl=this.fileUrl !== '' ? this.fileUrl : this.announForm.announcementFileUrl
      this.fileUrl = ''
      addAnnouncement(this.announForm).then(res => {
        if(res){
          this.$message.success('发布成功')
        }
      })
      this.form_status.status = 2;
      this.form_status.annoStatus = 1;
      addProject(this.form_status).then(res => {
        if(res){
          this.$message.success('项目状态更新成功')
        }
      })
      this.dialogFilePublicVisible=false
      this.init()
    },
    async handleLook(row) { // 查看
      this.form = row;
      await getUserByName(row.tenderOfficer).then(res => {
        this.form.phoneNumber = res.phoneNumber
        this.form.department = res.department
      })
      this.dialogViewVisible = true
    },
    handleAnnounFile(row){
      this.fileUrl = row
      this.$message.success('文件添加成功')
    },
    handleAnnounFileError(err, file, fileList) {
      // 上传失败的处理逻辑
      this.$message.error('文件上传失败，请重试');
    },
    //重置按钮
    reset() {
      this.procurementMethod = ''
      this.projectNumber = "";
      this.projectName = "";
      this.init();
    },
  }
}
</script>

<style scoped>
>>> .slide-right-dialog {
  top: -150px;
  right: 0;
  margin: 0;
  position: absolute;
  height: 1000px;
  width: 600px;
  animation: slide-right 0.3s ease;
}
>>> .slide-right-dialog .el-dialog__title {
  font-size: 14px; /* 调整对话框正文内容的字体大小 */
}
</style>